#!/home/y/bin/perl

use FindBin qw/$Bin/;
use lib qw(lib ../lib);

use Data::Dumper qw(Dumper);
$Data::Dumper::Indent = 1;

use Test::Deep;
use Test::More qw/no_plan/;
use Test::Exception;

use_ok( "Farsail::Config" );

dies_ok { Farsail::Config->new } 'die when new without rootdir';

my $rootdir = "$Bin/conf";

dies_ok {
    package Farsail::Config;
    local $FARSAIL_CONFIG = "not.exists";
    package main;
    Farsail::Config->new($rootdir);
} 'die when config file not exists';

my $config = Farsail::Config->new($rootdir);
isa_ok($config, 'Farsail::Config', 'new');
is($config->rootdir, $rootdir, 'rootdir');

is_deeply(
    $config->{STATE}{INCLUDES},
    {
        "$rootdir/farsail.conf" => 1,
        "$rootdir/app/noah.conf" => 1,
        "$rootdir/plugin/plugin.conf" => 1
    },
    'include'
);

is_deeply(
    $config->section('plugin'),
    {
        'bar' => 'bar',
        'foo' => 'foo'
    },
    'section'
);

is_deeply(
    $config->database('farsail_proxydb'),
    {
        'user' => 'ywb',
        'host' => 'ywb',
        'dbname' => 'proxy',
        'dbd' => 'Pg'
    },
    'database($db_id)'
);

is_deeply(
    [sort keys %{$config->database}],
    [
        'farsail_db_1',
        'farsail_db_2',
        'farsail_db_3',
        'farsail_db_4',
        'farsail_proxydb'
    ],
    'database()');


is_deeply(
    $config->cluster('farsail'),
    {
        'nodes' => [
            'farsail_db_1',
            'farsail_db_2',
            'farsail_db_3',
            'farsail_db_4'
        ],
        'proxy' => 'farsail_proxydb'
    },
    'cluster($cluster_name)'
);

is_deeply(
    [sort keys %{$config->cluster}],
    [
        'farsail',
        'test'
    ],
    'cluster()');

is_deeply(
    $config->pbs(),
    {
        'nodes' => [
            'ywb'
        ],
        'queue' => 'batch'
    },
    'pbs()');

is(join("\n", sort split /\n/, $config->log4perl)."\n", <<EOT, 'log4perl');
log4perl.appender.Daily = Log::Log4perl::Appender::File
log4perl.appender.Daily.Filter = Info
log4perl.appender.Daily.filename = $rootdir/../log/daily.log
log4perl.appender.Daily.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Daily.layout.ConversionPattern = %d %p - %m%n
log4perl.appender.Detail = Log::Log4perl::Appender::File
log4perl.appender.Detail.filename = $rootdir/../log/farsail_detail.log
log4perl.appender.Detail.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Detail.layout.ConversionPattern = %d %p - %m%n
log4perl.appender.File = Log::Log4perl::Appender::File
log4perl.appender.File.Filter = Info
log4perl.appender.File.filename = $rootdir/../log/farsail.log
log4perl.appender.File.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.File.layout.ConversionPattern = %d %p - %m%n
log4perl.appender.Monthly = Log::Log4perl::Appender::File
log4perl.appender.Monthly.Filter = Info
log4perl.appender.Monthly.filename = $rootdir/../log/monthly.log
log4perl.appender.Monthly.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Monthly.layout.ConversionPattern = %d %p - %m%n
log4perl.appender.Screen = Log::Log4perl::Appender::Screen
log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Screen.layout.ConversionPattern = %d %p - %m%n
log4perl.appender.Weekly = Log::Log4perl::Appender::File
log4perl.appender.Weekly.Filter = Info
log4perl.appender.Weekly.filename = $rootdir/../log/weekly.log
log4perl.appender.Weekly.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Weekly.layout.ConversionPattern = %d %p - %m%n
log4perl.filter.Info = Log::Log4perl::Filter::LevelRange
log4perl.filter.Info.AcceptOnMatch = true
log4perl.filter.Info.LevelMax = FATAL
log4perl.filter.Info.LevelMin = INFO
log4perl.logger = DEBUG, Screen
log4perl.logger.farsail.app.noah = DEBUG, Screen, File, Detail
EOT

